package com.tank.service.impl;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tank.bean.User;
import com.tank.bean.item.Talk;
import com.tank.core.TankModel;
import com.tank.dao.UserDAO;
import com.tank.service.MailService;
import com.tank.service.UserService;
import com.tank.util.MailUtil;
import com.tank.util.TankUtil;
import com.tank.vo.RetVo;

@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	@Qualifier("userDao")
	UserDAO dao;
	
	@Autowired
	@Qualifier("mailService")
	MailService mailSerivce;

	public TankModel checkUser(User user) {
		TankModel tm=TankModel.newInstance();
		tm.setSuccess(false);
		tm.setMessage("不存在该用户!");
		
		List<User> us = dao.findUserByName(user.getName());

		if (us != null && us.size() > 0) {

			tm.setMessage("密码错误！");

			for (User u : us) {
				if(!u.getValid()){
					tm.setSuccess(false);
					tm.setMessage("邮箱未验证!");
					break;
				}
				if (user.getPass().equals(u.getPass())) {
					tm.setSuccess(true);
					tm.setMessage("登录成功！");
					saveLoginSession(u);
					break;
				}
			}

		}
		return tm;
	}

	private void saveLoginSession(User u) {
		System.out.println("=savesession=");
		ServletActionContext.getRequest().getSession().setAttribute("user", u);
	}

	public boolean logout() {
		System.out.println("=logout=");
		ServletActionContext.getRequest().getSession()
				.setAttribute("user", null);
		ServletActionContext.getRequest().getSession().removeAttribute("user");
		ServletActionContext.getRequest().getSession().invalidate();
		return true;
	}

	public boolean isValidUser(User user) {
		System.out.println("=isValidUser=");
		List<User> us = dao.findUserByName(user.getName());
		if (us != null && us.size() > 0)
			return false;
		return true;
	}

	public void registerUser(User user) {
		if (user == null)
			return;

		user.setFanCount(0);
		user.setFollowCount(0);
		user.setItemCount(0);
		user.setTalkCount(0);
		user.setWantCount(0);
		user.setPhoto("zgg/face/default.png");
		user.setCredit(10);
		user.setGrade(0);
		user.setValid(false);

		System.out.println("=registerUser=");
		dao.save(user);
	}

	public void addFollow(User user) {
		User me = TankUtil.getCurrentUser();
		dao.addFollow(me, user);
	}

	public void removeFollow(User user) {
		User me = TankUtil.getCurrentUser();
		dao.removeFollow(me, user);

	}

	public TankModel checkUserNameAndMail(User user) {
		TankModel tm=TankModel.newInstance();
		tm.setMessage("用户名可以注册!");
		
		List<User> us = dao.findUserByNameOrMail(user);

		if (us != null || !us.isEmpty()) {
			for (User u : us) {
				if (u.getName().equals(user.getName())) {
					tm.setMessage("用户名已经存在!");
					tm.setSuccess(false);
					break;
				}

				if (u.getMail().equals(user.getMail())) {
					tm.setMessage("邮箱已经注册过!");
					tm.setSuccess(false);
					break;
				}
				
				

			}
		}

		return tm;
	}

	public TankModel modify(User user) {
		TankModel tm=TankModel.newInstance();
		
		User cu=TankUtil.getCurrentUser();
		
		cu.setHome(user.getHome());
		cu.setSchool(user.getSchool());
		cu.setIntro(user.getIntro());
		
		
		dao.update(user);
		
		
		tm.setSuccess(true);
		tm.setMessage("用户信息修改成功!");
		return tm;
	}

	public TankModel modifyPass(String oldpass, String newpass) {
		TankModel tm=TankModel.newInstance();
		
		
		User cu=TankUtil.getCurrentUser();
		if(cu == null){
			tm.setSuccess(false);
			tm.setMessage("用户会话过期，请重新登录！");
		}else if(cu.getPass().equals(oldpass)){
			cu.setPass(newpass);
			dao.update(cu);
			tm.setSuccess(true);
			tm.setMessage("修改成功！");
		}else{
			tm.setSuccess(false);
			tm.setMessage("原密码有错！");
		}
		return tm;
	}

	public TankModel modifyBasic(User user) {
		TankModel tm=TankModel.newInstance();
		
		User cu=TankUtil.getCurrentUser();
		if(cu == null){
			tm.setSuccess(false);
			tm.setMessage("用户会话过期，请重新登录！");
		}else{
			
			cu.setHome(user.getHome());
			cu.setSchool(user.getSchool());
			cu.setIntro(user.getIntro());
			
			dao.update(cu);
			
			tm.setSuccess(true);
			tm.setMessage("修改成功！");
		}
		return tm;
	}

}
